<?php

namespace app\api\command;

use app\api\model\msg\ReminderModel;
use app\api\model\UserFoodModel;
use app\api\model\UserReminderMessage;
use app\api\model\UserRemindMessageModel;
use app\api\model\UserRemindModel;
use think\console\Command;
use think\console\Input;
use think\console\Output;

class UserRemind extends Command
{

    protected function configure()
    {
        $this->setName('test')->setDescription('提醒客户进行打卡');
    }

    protected function execute(Input $input, Output $output)
    {
        $num = $this->sendUserRemindMessage();
        $num2 = $this->sendUserReminderMessage();
        $output->writeln("一共发送打卡提醒消息:" . $num . '条,'."温馨提醒消息:" . $num2);
    }

    public function sendUserRemindMessage()
    {
        $list = UserRemindModel::all(['is_use' => 1]);

        $nowTime = time();
        $endTime = time() + 1800;
        $day = date('Y-m-d', time());
        $config = config('site');
        $message = [
            '1' => $config['breakfast_message'],
            '2' => $config['lunch_message'],
            '3' => $config['dinner_message']
        ];
        $i = 0;
        foreach ($list as $val) {

            $time = strtotime(date('Y-m-d', time()) . $val['remind_time']);
            log_print($time,$nowTime,$endTime);
            //if ($nowTime > $time && $time < $endTime) {

                $userMessage = UserRemindMessageModel::get(['user_id' => $val['user_id'], 'day' => $day, 'type' => $val['type']]);
                if (!$userMessage) {
                    //查看当前用户是否打卡
                    $userFood = UserFoodModel::get(['user_id' => $val['user_id'], 'type' => $val['type'], 'day' => $day]);
                    if (!$userFood) {
                        //当前用户未打卡，提醒打卡
                        $data = [
                            'user_id' => $val['user_id'],
                            'type' => $val['type'],
                            'day' => $day,
                            'message' => $message[$val['type']]
                        ];
                        $model = new UserRemindMessageModel();
                        $model->save($data);
                        $i++;
                    }
                }

            }
        //}

        return $i;
    }

    /*
     * 用户温馨提示消息
     */
    public function sendUserReminderMessage()
    {

        $list = ReminderModel::all(['status' => 1, 'repeat' => 0]);

        $nowTime = time();
        $endTime = time() + 1800;
        $day = date('Y-m-d', time());

        $i = 0;
        foreach ($list as $val) {
            if ($nowTime >= $val['time']) {

                $userMessage = UserReminderMessage::get(['user_id' => $val['user_id'], 'day' => $day, 'reminder_id' => $val['id']]);
                if (!$userMessage) {
                    $data = [
                        'user_id' => $val['user_id'],
                        'message' => $val['title'],
                        'is_read' => 0,
                        'reminder_id' => $val['id'],
                        'day' => $day
                    ];
                    $model = new UserReminderMessage();
                    $model->save($data);
                    $i++;
                    $ReminderModel = new ReminderModel();
                    $change = $ReminderModel::get($val['id']);
                    $change->status = 0;
                    $change->save();
                }
            }
        }

        $list = ReminderModel::all(['status' => 1, 'repeat' => 1]);

        foreach ($list as $val) {

            $val_time = strtotime(date('Y-m-d',time()) . $val['repeattime']);

            if ($nowTime >= $val_time && $nowTime < $val_time + 1800) {

                $userMessage = UserReminderMessage::get(['user_id' => $val['user_id'], 'day' => $day, 'reminder_id' => $val['id']]);
                if (!$userMessage) {
                    $data = [
                        'user_id' => $val['user_id'],
                        'message' => $val['title'],
                        'is_read' => 0,
                        'reminder_id' => $val['id'],
                        'day' => $day
                    ];
                    $model = new UserReminderMessage();
                    $model->save($data);
                    $i++;
                }
            }
        }

        return $i;
    }

}